﻿@using CleanArchitecture.Core.Helper
@using CleanArchitecture.Infrastructure.Models.AdminUsers
@model AdminUserModel
@{
    Layout = "_BackendLayout";
    ViewData["Title"] = ViewBag.PageTitle as string;
    int PwdMinLen = ViewBag.PasswordMinLen;
    int PwdMaxLen = ViewBag.PasswordMaxLen;
}
@section cssstyles {
    <style type="text/css">
        body {
            margin: 5px !important;
            display: flex;
            flex: auto;
            flex-direction: column;
            min-height: 0;
            background: #f0f2f5;
        }
    </style>
}
<div class="layui-container" style="background-color: #fff;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend>账号安全设置 - 更换密码</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" action="">
        <input name="@Model.nameof(m => m.Id)" class="layui-hide" value="@Model.Id" />
        @*Warning 不要给input控件赋初使值 value=@Model.PasswordStrongLevel 后台没有对数据初使化 *@
        <input name="@Model.nameof(m => m.PasswordStrongLevel)" class="layui-hide" />
        <div class="layui-form-item">
            <label class="layui-form-label">原密码</label>
            <div class="layui-input-inline">
                @*Warning 不要给input控件赋初使值 value=@Model.OldPassword 后台没有对数据初使化 *@
                <input type="password" name="@Model.nameof(m => m.OldPassword)" placeholder="请输入密码" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                @*Warning 不要给input控件赋初使值 value=@Model.OldPassword 后台没有对数据初使化 *@
                <input type="password" name="@Model.nameof(m => m.NewPassword)" placeholder="请输入新密码" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
            <div class="layui-form-mid layui-word-aux">新密码应在 @PwdMinLen - @PwdMaxLen 位之间</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="ConfirmNewPassword" placeholder="请再次输入新密码" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
            <div class="layui-form-mid layui-word-aux">新旧密码不能相同</div>
        </div>
        <div class="layui-form-item" style="text-align: center; margin-right: 100px;">
            <button class="layui-btn" lay-submit="" lay-filter="changePwd">确认修改</button>
        </div>
    </form>
</div>

@section scripts{
    <script type="text/javascript">
    layui.use(['form', 'element', 'notice'], function () {
        var form = layui.form,
            element = layui.element,
            notice = Util.notice !== null && Util.notice() !== null
                ? Util.notice() : layui.notice,
            PwdMinLen = @PwdMinLen,
            PwdMaxLen = @PwdMaxLen;
        var $OldPwd = $("input[name='OldPassword']"),
            $NewPwd = $("input[name='NewPassword']"),
            $StrongLevel = $("input[name='PasswordStrongLevel']"),
            $ConfirmPwd = $("input[name='ConfirmNewPassword']"),
            NewPwdMod = 0; //新密码强度校验
        $NewPwd.on('blur', function () {
            if ($(this).val().trim() === "") return false;
            NewPwdMod = Util.checkPasswordStrong($(this).val(), PwdMinLen, PwdMaxLen);
            let strong = "当前密码强度：";
            if (NewPwdMod > 0 && NewPwdMod < 2) {
                strong += "低";
                $StrongLevel.val("1"); //与后台数据库对应
            }
            else if (NewPwdMod >= 2 && NewPwdMod < 4) {
                strong += "中";
                $StrongLevel.val("2");
            }
            else if (NewPwdMod >= 4) {
                strong += "强";
                $StrongLevel.val("3");
            }
            else strong = "密码长度超出限定范围"; //returns 0 密码过长或过短
            layer.tips(strong, $(this), { tips: 1 });
        });

        form.on('submit(changePwd)', function (data) {
            //DavidLee 2019/12/31 Dont trim the password!
            //User May use space as the password
            if ($OldPwd.val() === $NewPwd.val()) {
                notice.error("新旧密码不能相同");
                return false;
            }
            if ($NewPwd.val() !== $ConfirmPwd.val()) {
                notice.error("新密码与确认密码不一致");
                return false;
            }
            if (NewPwdMod <= 0) {
                notice.error("密码长度超出限定范围");
                return false;
            }
            var postData = data.field;
            if (postData.hasOwnProperty("ConfirmNewPassword")) {
                delete postData["ConfirmNewPassword"];
            }
            httpapi.fetchPost('/api/backend/account/edit', postData).then(res => {
                if (!httpapi.checkResponse(res))
                    //输入信息校验错误
                    notice.error(res.message);
                else {//Success
                    notice.success(res.message);
                    Util.loading(true, 5000);
                    httpapi.fetchPost('/api/backend/logout').then(res => {
                        $.wait(2000).then(function () {
                            Util.MainWindowRedirect("/login");
                        });
                    }).catch(error => notice.error("对不起，出现未知错误，请您联系系统管理员！"));
                }
            }).catch(error => notice.error("对不起，出现未知错误，请您联系系统管理员！"));
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
    </script>
}
